﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Objects;
using System.Data.Common;
using System.Data;

using MINERP.MODEL.InfoConfig;

using Microsoft.Practices.EnterpriseLibrary.Common;
using Microsoft.Practices.EnterpriseLibrary.Data;


namespace MINERP.DAL.InfoConfig
{
    public class PrinterDAL:DALBase
    {
        private static ObjectQuery<printer> GetPrinter()
        {
            return DataEntites.printer;
        }


        /// <summary>
        /// Get Shop list
        /// </summary>
        /// <returns></returns>
        public static List<printer> GetPrinterList()
        {
            List<printer> list = new List<printer>();
            foreach (printer shop in GetPrinter())
            {
                list.Add(shop);
            }
            return list;
        }
        /// <summary>
        /// Add shop
        /// </summary>
        /// <param name="obj"></param>
        public static void AddPrinter(printer obj)
        {
            DataEntites.AddToprinter(obj);
            try
            {
                DataEntites.SaveChanges();
            }
            catch(Exception e)
            {
                string a = e.Message;
            }
        }
        /// <summary>
        /// Update Private
        /// </summary>
        /// <param name="obj"></param>
        public static void UpdatePrinter(printer obj)
        {
            printer  printer = DataEntites.printer.First(p => p.PrinterID == obj.PrinterID);
            printer.hotDirPath = obj.hotDirPath;
            printer.ICCPath = obj.ICCPath;
            printer.IsTransverse = obj.IsTransverse;
            printer.PDes = obj.PDes;
            printer.PDPI = obj.PDPI;
            printer.PLength = obj.PLength;
            printer.PName = obj.PName;
            printer.PType = obj.PType;
            printer.PWidth = obj.PWidth;
            DataEntites.SaveChanges();
        }

        /// <summary>
        /// Delete Printer
        /// </summary>
        /// <param name="obj"></param>
        public static void DeletePrinter(printer obj)
        {
            printer printer = DataEntites.printer.First(p => p.PrinterID == obj.PrinterID);
            DataEntites.DeleteObject(printer);
            DataEntites.SaveChanges();
        }

        public static printer GetPrinterObj(string name)
        {
            return DataEntites.printer.FirstOrDefault(p => p.PName == name);
        }

        public static void UpdatePapeTypeICC(int printerId,string paperTypeName,string ICCPath,int sharpValue)
        {
        
            DbCommand command;
            Database db = DatabaseFactory.CreateDatabase();
            command = db.GetStoredProcCommand("UpdatePaperTypeICC");
            db.AddInParameter(command, "printerid", DbType.Int32,printerId);
            db.AddInParameter(command, "papertypename", DbType.String, paperTypeName);
            db.AddInParameter(command, "iccpath", DbType.String, ICCPath);
            db.AddInParameter(command, "sharpvalue", DbType.Int32, sharpValue);
            db.ExecuteNonQuery(command);
 
        }

        public static PrinterInfo GetIccPath(int printerid, string papertypeName)
        {
            PrinterInfo printerInfo = new PrinterInfo();
            DbCommand command;
            Database db = DatabaseFactory.CreateDatabase();
            command = db.GetSqlStringCommand(string.Format("select a.ICCPath ,a.sharpvalue from PrinterICC a,papertype b where a.PrinterID ={0} and a.PaperTypeId = b.PaperTypeID and b.name ='{1}' ", printerid, papertypeName));
            using (IDataReader rdr = db.ExecuteReader(command))
            {
                if (rdr.Read())
                {
                    printerInfo.ICCPath = rdr.GetString(0);
                    printerInfo.SharpVale = rdr.GetInt32(1);
                }
            }
            return printerInfo;

        }
    }
}
